package com.opensource.baowebcon.controller;

import com.opensource.common.Result.ResponseResult;
import com.opensource.common.enums.AppHttpCodeEnum;
import com.opensource.common.utils.CommonUtil;
import com.opensource.datamodel.dto.IndexProductListDto;
import com.opensource.datamodel.dto.page.PageDto;
import com.opensource.datamodel.dto.page.PageInfo;
import com.opensource.datamodel.dto.result.resultDto;
import com.opensource.datamodel.entity.BidInfoEntity;
import com.opensource.datamodel.entity.ProductInfoEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author wzh
 * @date 2024/5/7
 * @apiNote
 */
@Api(tags = "理财产品功能")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController{
    /*首页三类产品列表*/
    @ApiOperation(value = "首页三类产品列表",notes = "一个新手宝，三个优选，三个散标产品")
    @GetMapping("/getIndexProductListInfo")
    public ResponseResult getIndexProductBaseInfo(){

        IndexProductListDto data = productInfoService.queryIndexPageProducts();
        return ResponseResult.okResult(data);

    }

    /*按产品类型分页查询*/
    @ApiOperation(value = "产品分页查询",notes = "按产品类型分页查询")
    @GetMapping("/getProductListAndPageInfo")
    public ResponseResult getProductListAndPageInfo(@RequestParam("productType") Integer productType,
                                                    @RequestParam(value = "curPage",required = false,defaultValue = "1") Integer curPage,
                                                    @RequestParam(value = "pageSize",required = false,defaultValue = "9") Integer pageSize
                                                    ){
        Boolean flag = false;
        PageDto data = null;
        if (productType != null && (productType == 0 || productType == 1 || productType == 2)){
            curPage = CommonUtil.defaultCurPage(curPage);
            pageSize = CommonUtil.defaultPageSize(pageSize);
            //分页处理，记录总数
            Integer recordNums = productInfoService.queryRecordNumsByType(productType);
            if (recordNums>0){
                //产品集合
                //构建row
                List<ProductInfoEntity> productInfos = productInfoService.queryByTypeLimit(productType,curPage,pageSize);

                //构建PageInfo
                PageInfo page = new PageInfo(curPage,pageSize,recordNums);
                data = new PageDto(productInfos, page);
                flag = true;
            }

        }

        return Boolean.TRUE.equals(flag)?ResponseResult.okResult(data):ResponseResult.errorResult(AppHttpCodeEnum.REQUEST_PRODUCT_TYPE_ERR);



    }

    /*查询某个产品的详情和投资记录*/
    @ApiOperation(value = "产品详情",notes = "查询某个产品的详细信息和投资5条记录")
    @GetMapping("/getProductDetailAndBid/{id}")
    public <T> ResponseResult getProductDetailAndBid(@PathVariable("id") Integer productId){
        Boolean flag = false;
        resultDto<ProductInfoEntity> data = null;
        if (productId!=null&&productId>0){
            flag = Boolean.TRUE;
            //  查询产品信息
            ProductInfoEntity productInfo = productInfoService.queryById(productId);
            if (Objects.nonNull(productInfo)){
             //查询投资记录
                List<BidInfoEntity> bidInfoEntities = investInfoService.queryBidListByProductId(productId,1,5);
                data = new resultDto<>(bidInfoEntities, productInfo);
            }
        }




        return Boolean.TRUE.equals(flag)?ResponseResult.okResult(data):ResponseResult.errorResult(AppHttpCodeEnum.REQUEST_PRODUCT_TYPE_ERR);
    }



}
